package org.soarsky.wealthchengdu.imageCache;

import org.soarsky.wealthchengdu.cache.Cache;
import org.soarsky.wealthchengdu.cache.DefaultCache;
import org.soarsky.wealthchengdu.util.ThumbnailUtil;
import org.soarsky.wealthchengdu.util.Utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
/*
 * 数据缓存帮助类
 */
public class CacheMemUtil {	
	
	//缓存大小
    public static final long DEFAULT_CACHE_SIZE = Math.min(Runtime.getRuntime().maxMemory() / 4,  16 * 1024 * 1024); 
    private final static String					imageCacheName		= "imgCache";
	/**
	 * 内存缓存
	 */
	private  static Cache<String, Bitmap>	 cacheImage = null;	
	private  static String rootDir = null;
	
	static {
		cacheImage			= new DefaultCache<String, Bitmap>(imageCacheName, DEFAULT_CACHE_SIZE, 0);
		rootDir = Utils.getCacheDir(imageCacheName);
		if(rootDir != null) {
			CacheFileUtil.maintainCacheFile(rootDir);
		}
	}
	
	/**
	 * 插入图片缓存
	 * 
	 * @param name
	 * @param img
	 */
	public static void putImage2Cache(String name, Bitmap img)
	{
		cacheImage.put(name, img);
	}

	/**
	 * 根据名称从cache中获取图片（第一步，主线程，直接加载）
	 * 
	 * @param name
	 * @return
	 */
	public static Bitmap getImageFromCache(String name)
	{
		
		return cacheImage.get(name);
	}

	/**
	 * 根据名称从cache中清理图片
	 * 
	 * @param name	 * 
	 */
	public static void removeImageFromCache(String name)
	{		
		cacheImage.remove(name);
	}
	
	/**
	 * 从本地获取图片（第二步，异步，避免滑动时发卡）
	 * 
	 * @param name
	 * @return
	 */
	public static Bitmap getImage(String name)
	{
//		Bitmap bm = getImageFromCache(name);
//		if (bm == null)
//		{
//			bm = CacheFileUtil.loadImageFromFile(rootDir, name); // 到本地找
			Bitmap bm = CacheFileUtil.loadImageFromFile(rootDir, name); // 到本地找
			if (bm != null)
			{
//				Log.e("ggggg", "it's file");
				putImage2Cache(name, bm);
			}
//			else{
//				Log.e("ggggg", "It's network");
//			}
//		}
//		else{
//			Log.e("ggggg", "It's memory");
//		}
		return bm;
	}
	
	public static String getImagePath(String name){
		return CacheFileUtil.getImagePathForRoot(rootDir, name);
	}

	/**
	 * 把图片缓存到本地和内存
	 * 
	 * @param name
	 * @return
	 */
	public static void putImage(String name, byte[] data)
	{
		try {			
			Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
			putImage2Cache(name, bm);
			CacheFileUtil.saveByte2SDCard(data, rootDir, name);
		} catch (OutOfMemoryError oom) {
			clearCache();
			Bitmap bm = ThumbnailUtil.getImageThumbnail(data, 400, 400);	
			CacheFileUtil.saveByte2SDCard(Utils.bitmap2Bytes(bm), rootDir, name);
//			clearCache();
			putImage2Cache(name, bm);
		} catch (Exception e) {	
			Log.e("ooooo", e.toString());
			e.printStackTrace();
		}		
	}

	
	/**
	 * 把图片从本地和内存中清除
	 */
	public static void removeImage(String name) {
		removeImageFromCache(name);
		CacheFileUtil.removeFileByFileName(rootDir, name);
	}
	
	/**
	 * 清除cache
	 */
	public static void clearCache()
	{
		cacheImage.clear();
	}
	
	/**
	 * 清除所有图片缓存
	 */
	public static void removeImages(){
		CacheFileUtil.clearFiles(rootDir);
		clearCache();
	}
	
	/**
	 * 返回本地缓存所占容量
	 * @return
	 */
	public static String getDirSize(){
		try {
			return CacheFileUtil.FormetFileSize(rootDir);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	
}
